package com.pk.platform.util;

import java.io.ByteArrayInputStream;
import java.sql.Blob;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

import org.hibernate.Hibernate;

import com.pk.platform.util.constant.Constant;

public class StringConverter {

	/**
	 * <p>
	 * 说明:保留n位小数
	 * </p>
	 * @param s 需要格式化的字符串或double类型数据
	 * @param n 保留位数
	 * @return
	 */
	public static String toNBitString(Object s, int n)
	{
		if(n < 0)
			return "";
		
		String zero = ".";
		for(int i = 0; i < n; i++) //保留位数初始化
		{
			zero += "0";
		}
		
		if(".".equals(zero))
			zero = "";
		
		String result = "";
		try
		{
			DecimalFormat df = new DecimalFormat("#0" + zero); //位数表达式
			Double d = 0.0;
			if(s instanceof String) //如果是字符串
			{
				d = Double.parseDouble((String)s);
			}
			else if(s instanceof Double) //如果是double类型数据
			{
				d = (Double)s;
			}
			result = df.format(d); //结果
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		return result;
	}
	
	
	
	/**
	 * <p>
	 * 说明:string[]转Integer[]
	 * </p>
	 * @param str
	 * @return
	 */
	public static Integer[] stringToInteger(String[] str)
	{
		int length = str.length;
		Integer[] result = new Integer[length];
		for(int i = 0; i < length; i++)
		{
			result[i] = Integer.parseInt(str[i]);
		}
		return result;
	}
	
	
	/**
	 * <p>
	 * 说明:string[]转Long[]
	 * </p>
	 * @param str
	 * @return
	 */
	public static Long[] stringToLong(String[] str)
	{
	    int length = str.length;
	    Long[] result = new Long[length];
	    for(int i = 0; i < length; i++)
	    {
	        result[i] = Long.parseLong(str[i]);
	    }
	    return result;
	}
	
	
	/**
	 * <p>
	 * 说明:把长的字符变为短字符并加...
	 * </p>
	 * @param str
	 * @param n
	 * @return
	 */
	public static String toShortString(String str, int n)
	{
		if(str == null)
		{
			return "";
		}
		if(str.length() > n)
		{
			str = str.substring(0, n) + "...";
		}
		return str;
	}
	
	
	/**
	 * <p>
	 * 说明:将String数组组装成字符串如'1','2','3','4'
	 * </p>
	 * @param sArray
	 * @return
	 */
    public static String arrayToString(String[] sArray) {
        StringBuffer sb = new StringBuffer();
        for(String s : sArray){
            sb.append(",'"+s+"'");
        }
        String str = sb.toString();
        if (str.length() != 0 ) {
            return str.substring(1);
        }
        else {
            return "";
        }
       
    }
    
    
    /**
     * <p>
     * 说明:将Integer数组组装成字符串如1,2,3,4
     * </p>
     * @param sArray
     * @return
     */
    public static String arrayToString(Integer [] iArray) {
        StringBuffer sb = new StringBuffer();
        for(Integer i : iArray){
            sb.append("," + i);
        }
        String str = sb.toString();
        if (str.length() != 0 ) {
            return str.substring(1);
        }
        else {
            return "";
        }
       
    }
    
    
    /**
     * <p>
	 * 说明:组装成逗号形式,如1,2,3
	 * </p>
     * @param array
     * @return
     */
    public static String arrayToIntString(String[] array) {
    	StringBuffer sb = new StringBuffer();
    	for(String s : array){
    		sb.append("," + s);
    	}
    	String str = sb.toString();
    	if (str.length() != 0 ) {
    		return str.substring(1);
    	}
    	else {
    		return "";
    	}
    }
    
    
    
    /**
     * <p>
	 * 说明:删除字符串里的值
	 * </p>
     * @param str 原字串
     * @param value 要删除的值
     * @param split 分隔符
     * @return
     */
    public static String removeValue(String str, String value, String split)
    {
    	String s = str;
    	if(str.equals(value))
    		return "";
    	if(str.indexOf(value) < 0)
    		return s;
    	
    	int index = str.indexOf(value);
    	if(index == 0)
    	{
    		s = str.substring(value.length() + split.length());
    	}
    	else
    	{
    		s = str.substring(0, index - split.length()) + str.substring(index + value.length());
    	}
    	return s;
    }
    
    
    /**
     * <p>
     * 说明:补零
     * </p>
     * @param length 补零后的长度
     * @param source 需要补零的字符串
     * @return
     */
    public static String fillZero(int length, String source)
    {
        StringBuilder result = new StringBuilder(source);
        for(int i = result.length(); i < length; i++)
        {
            result.insert(0, '0');
        }
        return result.toString();
    }
	
    /**
     * 将字符串转换成html格式
     * @param textAreacontent 将要转换的字符串
     * @return
     */
    public static String convertToHtml(String textAreacontent){
    	String newStr = "";
    	if(textAreacontent != null && !"".equals(textAreacontent))
    	{
    		newStr = textAreacontent.replaceAll( "\\r\\n ", "<br/>").replaceAll("\\r", "<br/>").replaceAll(" ", "&nbsp;");
    		return newStr;
    	}
    	else
    	{
    		return newStr;
    	}
    }
    
    
    /**
     * <p>
     * 说明:附件上传产生的随机文件名
     * </p>
     */
    public static String RandomId(int sLen) {
        String date = new SimpleDateFormat("yyyyMMddHHmmss").format(Calendar
                .getInstance().getTime());
        String base = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        StringBuffer temp = new StringBuffer();
        temp.append(date);
        for (int i = 0; i < sLen; i++) {
            int p = (int) (Math.random() * 37);
            if (p > 35) {
                p = 35;
            }
            temp.append(base.substring(p, p + 1));
        }
        return temp.toString();
    }
    
    /**
     * @param map
     * @param field
     * @return
     */
    public static String listToString(List<Map<String, Object>> list, String field, String sign)
    {
    	StringBuffer result = new StringBuffer("");
    	for(Map<String, Object> map : list)
    	{
    		result.append(",").append(sign).append(map.get(field).toString()).append(sign);
    	}
    	String res = result.toString();
    	if(!"".equals(res))
    	{
    		res = res.substring(1);
    	}
    	return res;
    }
    
    /**
     * 非空判断
     * @param value
     * @return
     */
    public static boolean isNotNull(String value)
	{
		if(value != null && !"".equals(value.trim()))
		{
			return true;
		}
		return false;
	}
    
    
	public static String blobToString(Blob blob) {

		String result = "";
		try {
			if(blob == null)
				return result;
			ByteArrayInputStream msgContent = (ByteArrayInputStream) blob
					.getBinaryStream();
			byte[] byte_data = new byte[msgContent.available()];
			msgContent.read(byte_data, 0, byte_data.length);
			result = new String(byte_data, Constant.CHAR_SET);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	public static Blob stringToBlob(String str)
	{
		Blob blob = null;
		try
		{
			blob = Hibernate.createBlob(str.getBytes(Constant.CHAR_SET));
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		return blob;
	}
	
	public static String resultStr(String result, String message)
	{
		String str = "({result:'@resultVal', message:'@messageVal'})";
		str = str.replace("@resultVal", result);
		if(isNotNull(message))
		{
			str = str.replace("@messageVal", message);
		}
		else
		{
			str = str.replace("@messageVal", "");
		}
		return str;
	}
	
	public static String getFileExtendName(String name)
	{
		if(name != null && name.length() > 0)
		{
			int index = name.lastIndexOf(".");
			int len = name.length();
			if(index > -1 && index < len - 1)
			{
				return name.substring(index + 1);
			}
		}
		return "";
	}
	
}
